package com.ruoyi.base.service.impl;

import com.ruoyi.base.domain.BusWaterfuncarea;
import com.ruoyi.base.domain.BusWaterfuncareaSub;
import com.ruoyi.base.mapper.BusWaterfuncareaMapper;
import com.ruoyi.base.mapper.BusWaterfuncareaSubMapper;
import com.ruoyi.base.service.BusWaterfuncareaService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class BusWaterfuncareaServiceImpl implements BusWaterfuncareaService {
    @Autowired
    private BusWaterfuncareaMapper waterfuncareaMapper;

    @Autowired
    private BusWaterfuncareaSubMapper waterfuncareaSubMapper;

    /**
     * 查询水功能区
     *
     * @param id 水功能区主键
     * @return 水功能区
     */
    @Override
    public BusWaterfuncarea selectWqBaseWaterfuncareaById(String id)
    {
        BusWaterfuncarea wqBaseWaterfuncarea = waterfuncareaMapper.selectWqBaseWaterfuncareaById(id);
        //查询水功能区下拐点列表
        BusWaterfuncareaSub wqBaseWaterfuncareaSub = new BusWaterfuncareaSub();
        wqBaseWaterfuncareaSub.setFuncareaid(id);
        List<BusWaterfuncareaSub> subList = waterfuncareaSubMapper.selectWqBaseWaterfuncareaSubList(wqBaseWaterfuncareaSub);
        if (StringUtils.isNotEmpty(subList)){
            wqBaseWaterfuncarea.setSpinodalList(subList);
        }
        return wqBaseWaterfuncarea;
    }

    /**
     * 查询水功能区列表
     *
     * @param wqBaseWaterfuncarea 水功能区
     * @return 水功能区
     */
    @Override
    public List<BusWaterfuncarea> selectWqBaseWaterfuncareaList(BusWaterfuncarea wqBaseWaterfuncarea)
    {
        return waterfuncareaMapper.selectWqBaseWaterfuncareaList(wqBaseWaterfuncarea);
    }

    /**
     * 新增水功能区
     *
     * @param wqBaseWaterfuncarea 水功能区
     * @return 结果
     */
    @Override
    public int insertWqBaseWaterfuncarea(BusWaterfuncarea wqBaseWaterfuncarea)
    {
        wqBaseWaterfuncarea.setId(UUID.randomUUID().toString());
        if (null != wqBaseWaterfuncarea.getRegion()) {}
        //添加水功能区拐点信息
        if (StringUtils.isNotEmpty(wqBaseWaterfuncarea.getSpinodalList())) {
            List<BusWaterfuncareaSub> subList = wqBaseWaterfuncarea.getSpinodalList();
            for (BusWaterfuncareaSub sub : subList) {
                sub.setId(UUID.randomUUID().toString());
                sub.setFuncareaid(wqBaseWaterfuncarea.getId());
                sub.setFuncareaname(wqBaseWaterfuncarea.getFirstfuncarea());
                waterfuncareaSubMapper.insertWqBaseWaterfuncareaSub(sub);
            }
        }
        return waterfuncareaMapper.insertWqBaseWaterfuncarea(wqBaseWaterfuncarea);
    }

    /**
     * 修改水功能区
     *
     * @param wqBaseWaterfuncarea 水功能区
     * @return 结果
     */
    @Override
    public int updateWqBaseWaterfuncarea(BusWaterfuncarea wqBaseWaterfuncarea)
    {
        //先删除水功能区拐点信息，在添加新拐点
        if (StringUtils.isNotEmpty(wqBaseWaterfuncarea.getSpinodalList())) {
            waterfuncareaSubMapper.deleteWqBaseWaterfuncareaSubById(wqBaseWaterfuncarea.getId());
            List<BusWaterfuncareaSub> subList = wqBaseWaterfuncarea.getSpinodalList();
            for (BusWaterfuncareaSub sub : subList) {
                sub.setId(UUID.randomUUID().toString());
                sub.setFuncareaid(wqBaseWaterfuncarea.getId());
                sub.setFuncareaname(wqBaseWaterfuncarea.getFirstfuncarea());
                waterfuncareaSubMapper.insertWqBaseWaterfuncareaSub(sub);
            }
        }
        return waterfuncareaMapper.updateWqBaseWaterfuncarea(wqBaseWaterfuncarea);
    }

    /**
     * 批量删除水功能区
     *
     * @param ids 需要删除的水功能区主键
     * @return 结果
     */
    @Override
    public int deleteWqBaseWaterfuncareaByIds(String[] ids)
    {
        //先删除水功能区拐点信息，在删除基本信息
        waterfuncareaSubMapper.deleteWqBaseWaterfuncareaSubByIds(ids);
        return waterfuncareaMapper.deleteWqBaseWaterfuncareaByIds(ids);
    }

    /**
     * 删除水功能区信息
     *
     * @param id 水功能区主键
     * @return 结果
     */
    @Override
    public int deleteWqBaseWaterfuncareaById(String id)
    {
        //先删除水功能区拐点信息，在删除基本信息
        waterfuncareaSubMapper.deleteWqBaseWaterfuncareaSubById(id);
        return waterfuncareaMapper.deleteWqBaseWaterfuncareaById(id);
    }
}
